//获取应用实例
const app = getApp();

Page({
  data: {
    progressWidth: 0,
    progressTime: 60,
    mark: true,
    playPausetips: "开始"
  },
  onLoad: function () { 
    this.getTopCatList();
  },

  onShow(){
    this.getUserMsg();
  },
  //获取用户信息
  getUserMsg() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=getUserMsg',
      data: {
        token: app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            user_msg: res.data.data.user_msg,
          })
        } else {
          if (res.data.code == 105) {
            wx.removeStorage({
              key: 'token',
              success: function (res) {
                console.log(res)
              },
            })
            app.globalData.token = '';
            wx.reLaunch({
              url: '/pages/mine/phone-login',
            })
          }
          wx.showToast({
            title: '' + res.data.msg,
          })
        }
      }, fail: function () {

      }, complete: function () {
        wx.hideLoading();
      }
    });
  },

  //获取顶级商品分类列表
  getTopCatList() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=TaobaoCat&a=getTopCatList',
      data:
      {
      },
      header: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        that.setData({
          navData: res.data.data.list
        })
        that.getGoodsList();
      }, fail: function () {
        wx.hideLoading();
      }
    });
  },

  //获取推荐淘宝客商品列表
  getGoodsList() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Tbk&a=getTopList',
      data:
      {
        token: app.globalData.token,
      },
      header: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.data.list.length > 0) {
            that.setData({
              shopList: res.data.data.list
            })
        } else {
          that.setData({
            shopList: ""
          })
        }

      }, fail: function () {
        wx.hideLoading();
      }
    });
  },

shared:function(){
  wx.navigateTo({
    url: '/pages/mine/shared',
  })
},

  playbtn() {
    let that = this;
    let mark = that.data.mark;
    if (mark) {
      that.timer = setInterval(that.run, 1000); //that.timer关键点
      wx.showToast({
        title: '开始',
      })
      that.setData({
        mark: false,
        playPausetips: "暂停"
      })
    } else {
      clearInterval(that.timer);
      wx.showToast({
        title: '暂停',
      })
      that.setData({
        mark: true,
        playPausetips: "开始"
      })
    }
  },
  run() {
    let that = this;
    let totalProgressTime = 60 //秒
    let progressWidth = that.data.progressWidth; //显示进度
    let progressTime = that.data.progressTime; //时间

    if (progressWidth === 100) {
      wx.showToast({
        title: '结束回调处理',
      })
      clearInterval(that.timer);
      that.setData({
        progressTime: totalProgressTime,  //进度条需要总时间s
        progressWidth: 100, //进度100%
        progressTime: 60
      })
      return;
    }
    progressTime--;
    progressWidth = (totalProgressTime - progressTime) * (100 / 60)
    that.setData({
      progressWidth: progressWidth,
      progressTime: progressTime
    })
  },
  buyvip: function () {
    wx.navigateTo({
      url: '/pages/mine/vin-shop1',
    })
  },

  buyvip2: function () {
    wx.navigateTo({
      url: '/pages/mine/vin-shop2',
    })
  },

  //立即开通
  tobuy() {
    wx.navigateTo({
      url: '/pages/mine/order-pay?money=0.01',
    })
  },
  //立即开通
  tobuy1() {
    wx.navigateTo({
      url: '/pages/mine/order-pay?money=100',
    })
  },
});
